# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: MZ_IL.py
  @time: 2022/5/16 14:16
  @desc:
"""

import time
from math import *
from ATS.application.LCR_App.U2722A_App import U2722A_App
from ATS.application.LASER_App.N7744C_App import N7744C_App
from ATS.application.LASER_App.L8164B_App import L8164B_App
from utils.read_ini_2_dict import get_option_value, get_refrence_data, get_loss_data
from utils.get_wave_power_util import get_wave_power_value, get_wave_loss_gc
from utils.save_setting_2_ini import save_ini_info
from ATS.util.calcu_step_util import get_points_list
from ATS.util.save_data_2_csv_util import saveData2CSV
from matplotlib import pyplot as plt
from ATS.switchdriver.LightSwitchDriver import LightSwitchDriver
import numpy as np
from gui import constant


def mz_il(heater_channel, heater_channel_voltage, heater_channel_current_range, heater_channel_limit,
          n7744_range):
    l8164_inst = L8164B_App("SS3")
    u2722_inst = U2722A_App("SMU1")
    n7744_inst = N7744C_App("SS2")
    u2722_inst.set_voltage_range(heater_channel)
    u2722_inst.set_voltage(heater_channel_voltage, heater_channel)
    u2722_inst.set_current_range_limit(heater_channel_current_range, heater_channel_limit, heater_channel)
    n7744_inst.set_power_range(n7744_range)
    n7744_inst.set_power_unit()
    wavelength = l8164_inst.get_source_wavelength()
    input_value = l8164_inst.fetch_input_power()
    output_value = n7744_inst.get_power_value()
    wave_power_tuple = get_refrence_data()
    loss_sys = get_wave_power_value(wavelength, wave_power_tuple)
    loss_gc = get_wave_loss_gc(wavelength, get_loss_data())
    loss_mz = float(input_value) - float(output_value) - float(loss_sys) - 2 * float(loss_gc) - 2 * -13
    save_ini_info(constant.REMEMBER_VALUE, "remember_value", "loss_mz", loss_mz)
    return loss_mz
